from django.contrib import admin

# Register your models here.
from goods import models
from celery_tasks.html.tasks import generate_static_list_search_html


class GoodsCategoryAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        """
        钩子方法　用于在站点中编辑和添加数据　会自动执行
        :param request:　本次编辑和添加对的请求对象
        :param obj:本次编辑和添加的模型
        :param form:本次编辑和添加的表单系统对象
        :param change:本次编辑和添加的改动的对象
        :return:
        """
        obj.save()  # 保存本次的提交
        #         保存提交后　需要执行异步的任务
        generate_static_list_search_html.delay()

    def delete_model(self, request, obj):
        obj.delete()
        generate_static_list_search_html.delay()


class GoodsChannelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        """
        钩子方法，在运营站点中编辑/添加数据时，会自动执行代码
        :param request: 本次编辑/添加数据时的请求对象
        :param obj: 本次编辑/添加数据时的模型对象
        :param form: 本次编辑/添加数据时表单系统对象
        :param change: 本次编辑/添加数据时的改动的数据
        """
        obj.save()  # 保存本次提交的操作

        # 保存提交操作以后，执行异步任务
        generate_static_list_search_html.delay()

    def delete_model(self, request, obj):
        """
        钩子方法，在删除数据的时候自动执行
        :param request: 本次操作的请求对象
        :param obj: 本次操作的模型对象
        :return:
        """
        obj.delete()

        # 保存提交操作以后，执行异步任务
        generate_static_list_search_html.delay()


class SKUAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.id)


class SKUSpecificationAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.sku.id)

    def delete_model(self, request, obj):
        sku_id = obj.sku.id
        obj.delete()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(sku_id)


class SKUImageAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.sku.id)

        # 设置SKU默认图片
        sku = obj.sku
        if not sku.default_image_url:
            sku.default_image_url = obj.image.url
            sku.save()

    def delete_model(self, request, obj):
        sku_id = obj.sku.id
        obj.delete()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(sku_id)


admin.site.register(models.GoodsCategory, GoodsCategoryAdmin)
admin.site.register(models.GoodsChannel, GoodsChannelAdmin)
admin.site.register(models.Goods)
admin.site.register(models.Brand)
admin.site.register(models.GoodsSpecification)
admin.site.register(models.SpecificationOption)
admin.site.register(models.SKU, SKUAdmin)
admin.site.register(models.SKUSpecification, SKUSpecificationAdmin)
admin.site.register(models.SKUImage, SKUImageAdmin)
